<html>
<head><meta charset="utf-8"><title>Is reinerpreting a slice &amp;[u8] to &amp;[bool] UB? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html">Is reinerpreting a slice &amp;[u8] to &amp;[bool] UB?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="171758108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171758108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maik Klein <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171758108">(Jul 26 2019 at 08:54)</a>:</h4>
<p>Consider <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=9ccefa49b69b22fb0103e15f9f251470" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=9ccefa49b69b22fb0103e15f9f251470">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=9ccefa49b69b22fb0103e15f9f251470</a></p>



<a name="171761093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171761093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171761093">(Jul 26 2019 at 09:48)</a>:</h4>
<p><span class="user-mention" data-user-id="228832">@Maik Klein</span> it is UB <em>unless</em> you know for sure that every element in the slice is either 0 or 1</p>



<a name="171761104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171761104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171761104">(Jul 26 2019 at 09:49)</a>:</h4>
<p>IOW, the slice has to contain bytes that are "valid" for <code>bool</code></p>



<a name="171761177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171761177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171761177">(Jul 26 2019 at 09:50)</a>:</h4>
<p>However, this is based on the strictest possible resolution for <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/77" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/77">https://github.com/rust-lang/unsafe-code-guidelines/issues/77</a>. We might end up being more relaxed and decide that data behind references does not have to be valid until the reference is actually used (for some notion of "use"). But, until the lang team decided that this is <em>not</em> UB, it is UB (to remain forward compatible).</p>



<a name="171789894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171789894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maik Klein <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171789894">(Jul 26 2019 at 17:00)</a>:</h4>
<p>Thanks for the reply, just to be sure this would only apply to references and not pointers? So I could have worked around it by creating my own slice type?</p>
<div class="codehilite"><pre><span></span>pub struct SliceMut&lt;&#39;a, T&gt; {
    start: *mut T,
    len: usize,
    _marker: PhantomData&lt;&amp;&#39;a ()&gt;,
}
</pre></div>



<a name="171790105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171790105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171790105">(Jul 26 2019 at 17:02)</a>:</h4>
<p>by pointers you mean raw pointers?</p>



<a name="171790112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171790112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maik Klein <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171790112">(Jul 26 2019 at 17:02)</a>:</h4>
<p>yeah</p>



<a name="171790115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171790115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171790115">(Jul 26 2019 at 17:02)</a>:</h4>
<p><span class="user-mention" data-user-id="228832">@Maik Klein</span> you might be interested in <a href="https://github.com/rust-lang/rust/pull/60667" target="_blank" title="https://github.com/rust-lang/rust/pull/60667">https://github.com/rust-lang/rust/pull/60667</a> :)</p>



<a name="171839105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171839105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maik Klein <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171839105">(Jul 27 2019 at 10:14)</a>:</h4>
<p>Yeah that is essentially what I want thanks. Also much nice than my impl, I'll just steal it.</p>



<a name="171839427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171839427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171839427">(Jul 27 2019 at 10:24)</a>:</h4>
<p><span class="user-mention" data-user-id="228832">@Maik Klein</span> notice that this is the kind of code that is correct in libstd but not correct outside</p>



<a name="171839430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171839430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171839430">(Jul 27 2019 at 10:24)</a>:</h4>
<p>rustc reserves to change the way slices are represented etc, and libstd will be updated</p>



<a name="171839440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171839440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171839440">(Jul 27 2019 at 10:25)</a>:</h4>
<p>but code outside libstd cannot make assumptions about rustc implementation details the same way libstd does</p>



<a name="171840790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171840790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maik Klein <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171840790">(Jul 27 2019 at 11:06)</a>:</h4>
<p>Yeah I'd be fine to sync it manually until it would be on stable, but I just realized that unions with non copy fields are unstable as well</p>



<a name="171841446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20reinerpreting%20a%20slice%20%26%5Bu8%5D%20to%20%26%5Bbool%5D%20UB%3F/near/171841446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maik Klein <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20reinerpreting.20a.20slice.20.26.5Bu8.5D.20to.20.26.5Bbool.5D.20UB.3F.html#171841446">(Jul 27 2019 at 11:23)</a>:</h4>
<p>So I just go with my own slice impl until this is on stable</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>